<?php

if (!empty($_POST['save'])) {
	if(isset($_GET['id'])) {
	  $_SESSION['vendor']=new Vendor($_GET['id']);  
	  $db=Database::getInstance();
	  $sql="SELECT b.id FROM brands b WHERE b.nom='".$_SESSION['vendor']['nom']."'";
	  $result=$db->query($sql);
	  $_SESSION['brand']=new Brand($result[0]['id']);
	}
	else {
			$_SESSION['vendor']=new Vendor();                    
			$_SESSION['brand']=new Brand();
			}
			$_SESSION['vendor']['nom']=$_POST['name'];
			$_SESSION['brand']['nom']=$_POST['name'];
			$_SESSION['vendor']['raison_sociale']=$_POST['social'];
			$_SESSION['vendor']['forme']=$_POST['form'];
			$_SESSION['vendor']['siret']=$_POST['siret'];
			$_SESSION['vendor']['tva_intra']=$_POST['num_tva'];
			
			$_SESSION['vendor']['adresse']=$_POST['adress'];
			$_SESSION['vendor']['cp']=$_POST['cp'];
			$_SESSION['vendor']['ville']=$_POST['city'];
			$_SESSION['vendor']['responsable']=$_POST['contact'];
			$_SESSION['vendor']['tel']=$_POST['tel'];
			$_SESSION['vendor']['tel_portable']=$_POST['mobile'];
			$_SESSION['vendor']['rib1']=$_POST['rib1'];
			$_SESSION['vendor']['rib2']=$_POST['rib2'];
			$_SESSION['vendor']['rib3']=$_POST['rib3'];
			$_SESSION['vendor']['rib4']=$_POST['rib4'];
			$_SESSION['vendor']['marge']=$_POST['margin'];
			$_SESSION['vendor']['password']=md5($_POST['pass']);
			$_SESSION['vendor']['slug']=$_POST['slug'];
			$_SESSION['brand']['slug']=$_POST['slug'];
			$_SESSION['vendor']['hash']=md5($_POST['name']);
			
			$_SESSION['vendor']['email']=$_POST['login'];
			if(!empty($_POST['tva'])){
				if ($_POST['tva']=='non'){$_SESSION['vendor']['tva']=0;}
				if ($_POST['tva']=='oui'){$_SESSION['vendor']['tva']=1;}
			}
			if(!empty($_POST['pays'])){
				$_SESSION['vendor']['pays_id']=$_POST['pays'];
			}
			if(!empty($_POST['email2'])){
				$_SESSION['vendor']['email2']=$_POST['email2'];
			}
			if(!empty($_POST['email3'])){
				$_SESSION['vendor']['email3']=$_POST['email3'];
			}
			$_SESSION['brand']->save();
			$_SESSION['vendor']->save();
				
			
			$commercant=$_SESSION['vendor'];
			$commercant->load();
			$tpl->assign('commercant',$commercant);
		
			$vendors=Vendor::getVendors();
			$tpl->assign('vendors',$vendors);
			
	}
?>	